package cy.jsonview.listener;

import cy.jsonview.code.Tool;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;

import javax.swing.JTree;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

/**
 * 
 * @author zyd
 * @param expand,为true就是展开树，为false就是收起树
 *
 */

public class ExpandTreeActionListener implements ActionListener {
	private final JTree tree;
	private boolean expand;
	private TreePath nodePath=null;
	
	public ExpandTreeActionListener(JTree tree,boolean expand) {
		this.tree= tree;
		this.expand=expand;
	}
	
	public ExpandTreeActionListener(JTree tree,TreePath nodePath,boolean expand) {
		this.tree= tree;
		this.expand=expand;
		this.nodePath=nodePath;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		if (nodePath== null) {
			TreeNode node = (TreeNode) tree.getModel().getRoot(); 
			Tool.expandAll(tree, new TreePath(node), expand); 
		}else {
			Tool.expandItem(tree, nodePath, expand);
		}
		
		
		
	}
	
	
	
}
